//
//  WaWaYaYaRewardManager.m
//  KidReading
//
//  Created by YangShuai on 16/11/19.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "WaWaYaYaRewardManager.h"
#import "ConstDefine.h"

#define __WaWaCoinState @"__WaWaCoinState"
#define __LocalWaWaCoin @"__LocalWaWaCoin"
@interface WaWaYaYaRewardManager ()

@end

@implementation WaWaYaYaRewardManager
//
static WaWaYaYaRewardManager* singleManager = nil;
+(WaWaYaYaRewardManager *)defaultManager{
    if (!singleManager) {
        @synchronized (self) {
            singleManager = [[WaWaYaYaRewardManager alloc] init];
        }
    }
    return singleManager;
}

//手动调用
-(void)WaWaYaYaRewardWithActCode:(NSString*)actCode Param:(NSString*)param Success:(void (^)(NSDictionary * retDict))success Failure:(void (^)(NSError *error))failure{
    [[NetGetPostsWithCache shareInstance] rewardActionWithActCode:actCode Param:param Success:^(NSDictionary *retDict) {
        if (success) {
            success(retDict);
        }
    } Failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//
//有未领取的奖励
-(void)logRewardEventIsNeedHandle:(BOOL)isNeedHandle ActCode:(NSString*)actCode Param:(NSString*)param{
    if (isNeedHandle) {
        //需要本地处理
        [self WaWaYaYaRewardWithActCode:actCode Param:param Success:nil Failure:nil];
    }else{
        //接口自动处理
    }
}
//添加小红点
-(void)addRedPointWithServerRetTure{
    [self updateWaWaCoinState:YES];
    if ([self getWaWaCoinState]) {
        [self reflashRedPoint];
    }
}

#pragma - mark 首页红点状态
-(void)resetWaWaCoinState{
    [self updateWaWaCoinState:NO];
    if (![self getWaWaCoinState]) {
        [self reflashRedPoint];
    }
}
-(void)updateWaWaCoinState:(BOOL)state{//1 有 0 无
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:@(state) forKey:[NSString stringWithFormat:@"%@_%@",__WaWaCoinState,@(uinfo.uid)]];
}
-(BOOL)getWaWaCoinState{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    return [[userDefaults objectForKey:[NSString stringWithFormat:@"%@_%@",__WaWaCoinState,@(uinfo.uid)]] boolValue];
}
#pragma - mark 存储本地单独WaWa币
-(NSInteger)getLocalWaWaCoin{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    return [[userDefaults objectForKey:[NSString stringWithFormat:@"%@_%@",__LocalWaWaCoin,@(uinfo.uid)]] integerValue];
}
-(BOOL)hasLocalWaWaCoin{
    BOOL hasLocal = NO;
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSNumber* wawaCoin = [userDefaults objectForKey:[NSString stringWithFormat:@"%@_%@",__LocalWaWaCoin,@(uinfo.uid)]];
    if (wawaCoin) {
        hasLocal = YES;
    }
    return hasLocal;
}
-(void)updateLocalWaWaCoin:(NSInteger)WaWaCoin{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    [self resetWaWaCoinState];
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:@(WaWaCoin) forKey:[NSString stringWithFormat:@"%@_%@",__LocalWaWaCoin,@(uinfo.uid)]];
}
//
-(void)reflashRedPoint{
//    [[RootViewController shareLastInstance] reflashHomePage];
//    [[RootViewController shareLastInstance] refreshAboutMeState];
//    [[[RootViewController shareLastInstance] getHomePageVC] reflashCanCollectCoinFlag];
    //[[RootViewController shareLastInstance] checkHomepageIconRedPointWithState:[self getWaWaCoinState] Code:@"homepage"];
    //[[RootViewController shareLastInstance] checkHomepageIconRedPointWithState:[self getWaWaCoinState] Code:@"me"];
}

//故事卡
-(void)updateFragmentStoryCardCount:(NSInteger)fragmentStoryCardCount CompletedStoryCardCount:(NSInteger)completedStoryCardCount{
    self.completedFirstUpdate = YES;
    self.fragmentStoryCardCount = fragmentStoryCardCount;
    self.completedStoryCardCount = completedStoryCardCount;
}



@end
